<template>
  <div v-loading="loading" class="device-list-main">
    <!-- filter start -->
    <el-form ref="filterForm" :inline="true" :model="filterForm" label-width="120px" size="small">
      <el-form-item
        prop="deviceName"
        label="终端名称："
      >
        <el-input
          v-model="filterForm.deviceName"
          placeholder="请输入"
          clearable
          class="app-w-350"
        />
      </el-form-item>
      <el-form-item
        prop="deviceId"
        label="酷点终端编号："
      >
        <el-input
          v-model="filterForm.deviceId"
          placeholder="请输入"
          clearable
          class="app-w-350"
        />
      </el-form-item>
      <el-form-item
        prop="shopId"
        label="店铺："
      >
        <el-select v-model="filterForm.shopId" class="app-w-350" filterable clearable placeholder="请选择">
          <el-option label="全部店铺" value />
          <el-option
            v-for="item in shopOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        prop="sn"
        label="机具SN号："
      >
        <el-input
          v-model="filterForm.sn"
          placeholder="请输入"
          clearable
          class="app-w-350"
        />
      </el-form-item>
      <div>
        <el-form-item label=" ">
          <el-button type="primary" round @click="submitFilterForm">查询终端</el-button>
          <el-button round @click="resetFilterForm">重置条件</el-button>
        </el-form-item>
      </div>
    </el-form>

    <!--  列表  -->
    <div v-if="!loading&&list.length===0" class="empty-tips">
      查询暂无数据
    </div>
    <el-row :gutter="16" type="flex">
      <el-col v-for="item in list" :key="item.kdDeviceNo" :md="12" :lg="8" :xl="6">
        <el-card :body-style="{ padding: '20px 15px'}">
          <div class="name app-ellipsis-l1">
            {{ item.alipayShopName }}
          </div>
          <div class="more-info">
            <div class="app-ellipsis-l1">酷点终端：{{ item.kdDeviceNo || '-' }}</div>
            <div class="app-ellipsis-l1">酷点店铺：{{ item.shopName || '-' }}</div>
            <div class="app-ellipsis-l1">支付宝店铺：{{ item.alipayShopId || '-' }}</div>
            <div class="app-ellipsis-l1">机具SN号：{{ item.deviceSn || '-' }}</div>
            <div class="app-ellipsis-l1">备注：{{ item.remark || '-' }}</div>
          </div>
          <div class="status-info">
            <span class="app-color-primary">绑定成功</span>
          </div>
          <div class="handle-group">
            <el-button type="text" class="app-color-danger" size="mini" icon="el-icon-edit" circle @click.native="handleUnBind(item)"> 解绑
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!--分页-->
    <PaginationDirection
      v-if="paginationConfig.total!==0"
      :config.sync="paginationConfig"
      @sizesChange="fetchData"
      @pageChange="fetchData"
    />

    <!--组件-->
    <MchRuYiLiteFormDialog ref="mchRuYiLiteFormDialog" />

  </div>
</template>

<script>
import MchRuYiLiteFormDialog from './MchRuYiLiteFormDialog'
import { allShopList } from '@/api/mch/shop'
import { ruYiLiteList, unbindRuYiLite } from '@/api/mch/equipment'

export default {
  components: {
    MchRuYiLiteFormDialog
  },
  data() {
    return {
      // 配置
      // 分页配置
      paginationConfig: {
        direction: 'center', // 分页位置
        layout: 'total, sizes, prev, pager, next, jumper', // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10 // 每页条数-默认10
      },
      // 状态
      loading: false,
      // 数据
      shopOptions: [],
      filterForm: {
        businessId: null,
        deviceId: null,
        deviceName: null,
        shopId: null,
        sn: null,
        pageNum: 1,
        pageSize: 10
      },
      list: []
    }
  },
  computed: {
  },
  watch: {},
  beforeCreate() {
  },
  created() {
    // 定义全局刷新方法
    this.$root.$on('refreshMchEquipmentRuYiLiteList', () => this.fetchData())
  },
  beforeMount() {
  },
  mounted() {
    this.fetchAllShopOptions()
    this.fetchData()
  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
    this.$root.$off('refreshMchEquipmentRuYiLiteList')
  },
  destroyed() {
  },
  methods: {
    /**
     * 数据组
     */
    // 获取列表
    fetchData(
      page = this.paginationConfig.page,
      pageSize = this.paginationConfig.pageSize
    ) {
      // 请求参数
      const params = Object.assign({}, this.filterForm)
      params.pageNum = page
      params.pageSize = pageSize
      params.businessId = this.$route.query.mchId
      // 清除多余字段s
      for (const key in params) {
        if (this.$isNull(params[key]) || Array.isArray(params[key]) && params[key].length === 0) delete params[key]
      }
      // 请求
      this.loading = true
      ruYiLiteList({ data: params })
        .then(res => {
          this.list = res.data.list || []
          this.paginationConfig.total = res.data.total
          this.paginationConfig.page = res.data.pageNum
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 获取所有门店
    fetchAllShopOptions() {
      const params = {
        businessId: this.$route.query.mchId
      }
      this.loading = true
      allShopList({ params }).then(res => {
        this.shopOptions = res.data || []
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 按钮组
     */
    // 查询
    submitFilterForm() {
      this.fetchData(1)
    },

    // 重置
    resetFilterForm() {
      this.$refs.filterForm.resetFields()
    },

    // 编辑
    handleUnBind(row) {
      // this.$refs.mchRuYiLiteFormDialog.open({ mode: 'unBind', row }).then()
      this.$confirm(
        `确定解绑终端【${row.kdDeviceNo}】 ?`,
        '温馨提示',
        {
          confirmButtonText: '解绑',
          cancelButtonText: '取消',
          type: 'warning',
          center: true,
          roundButton: true
        }
      ).then(() => {
        const params = {
          id: row.id
        }
        this.loading = true
        unbindRuYiLite({ params }).then(() => {
          this.fetchData()
          this.$notify({
            title: '成功',
            message: '解绑成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
        })
      }).finally(() => {
        this.loading = false
      })
    }
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.device-list-main {
  padding-left: 20px;
  /*reset*/
  .el-row {
    flex-wrap: wrap;

    .el-col {
      margin-bottom: 16px;
      height: 100%;
    }
  }

  /*卡片*/
  .el-card {
    position: relative;
    border-radius: 20px;

    .handle-group {
      position: absolute;
      right: 10px;
      top: 5px;
      //display: none;
    }

    //:hover .handle-group {
    //  display: block;
    //}

    /*title*/
    .title {
      height: 50px;
      line-height: 50px;
      font-size: 18px;
      font-weight: bold;
    }

    /*card*/
    .name {
      font-size: 18px;
    }

    .more-info {
      font-size: 14px;
      color: #909399;

      div {
        margin-top: 10px;
      }
    }

    .status-info {
      margin-top: 10px;
      padding-top: 10px;
      border-top: 1px solid #f5f5f5;
      color: #909399;
    }
  }

  /*空提示*/
  .empty-tips {
    text-align: center;
    font-size: 12px;
    padding-top: 100px;
  }
}

</style>
